const dbConnection = require('../utils/GetConnection');

module.exports = {
    getJsapiTicket: () => {
        return new Promise((resolve,reject)=>{
            dbConnection("select ticket,expires_in from wechat_jsapi_ticket where id = ?",[1],(err,result)=>{
                if(err){
                    reject(err);
                }
                resolve(result);
            })
        })
    },
    saveJsapiTicket:(ticketInfo)=>{
        return new Promise((resolve,reject)=>{
            dbConnection("replace into wechat_jsapi_ticket(id,ticket,expires_in) values(?,?,?) ",[1,ticketInfo.ticket,ticketInfo.expires_in * 1000 + Date.now()],(err)=>{
                if(err){
                    reject(err);
                    return;
                }
                resolve();
            })
        })
    }
};